#!/bin/bash

set -x 
exec &> /server/log/redis_result.txt
#安装redis
mkdir /data/soft -p
cd /data/soft/
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
tar zxf redis-5.0.7.tar.gz -C /opt/
cd /opt
ln -s /opt/redis-5.0.7 /opt/redis
cd /opt/redis 
make 
make install

#编写配置文件
mkdir -p /opt/redis_6379/{conf,logs,pid} 
mkdir -p /data/redis_6379
cat >/opt/redis_6379/conf/redis_6379.conf<<EOF
daemonize yes 
bind 127.0.0.1 `ip a s ens33 | sed -n 3p | awk '{print $2}' | awk -F "/" '{print $1}'`
port 6379 
requirepass 123456
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
EOF

#启动redis服务
#redis-server /opt/redis_6379/conf/redis_6379.conf

#配置启动脚本
groupadd redis -g 1002
useradd redis -u 1002 -g 1002 -M -s /sbin/nologin
chown -R redis:redis /opt/redis*
chown -R redis:redis /data/redis*
cat >/etc/systemd/system/redis.service<<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload 
systemctl start redis
systemctl enable redis.service

#kjcs.sh启动脚本编辑
cat >>/etc/systemd/system/kjcs_sh.service<<EOF 
[Unit]
Description=/server/ldsinstall/kjcs.sh  Compatibility
ConditionPathExists=/server/ldsinstall/kjcs.sh
After=network.target auditd.service
 
[Service]
Type=forking
ExecStart=/server/ldsinstall/kjcs.sh start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kjcs_sh.service



#ws_start.sh启动脚本编辑
cat >>/etc/systemd/system/ws_start.service<<EOF 
[Unit]
Description=/server/ldsinstall/ws_start.sh  Compatibility
ConditionPathExists=/server/ldsinstall/ws_start.sh
After=network.target auditd.service
 
[Service]
Type=forking
ExecStart=/server/ldsinstall/ws_start.sh start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ws_start.service